﻿using System.Collections.Generic;
using System.Transactions;
using Bootstrap;
using Bootstrap.AutoMapper;
using Dogshow.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace Dogshow.Linq2SQLRepository.Test
{
    [TestClass]
    public class KinoloskaDrustvaRepositoryFixtures
    {
        private KinoloskaDrustvaRepository m_Repository;

        [ClassInitialize]
        public static void ClassInit(TestContext ctx)
        {
            var model = new DogshowModel();
            if (model.DatabaseExists())
            {
                model.DeleteDatabase();
            }
            model.CreateDatabase();
            model.ExecuteCommand("INSERT INTO Mesta VALUES ('21000', 'NOVI SAD')");
            model.ExecuteCommand("INSERT INTO Mesta VALUES ('11000', 'BEOGRAD')");
            model.ExecuteCommand("INSERT INTO KinoloskaDrustva VALUES ('KD Novi Sad', 'Oslobođenja 11', 1, '021-428-877', '021-428-877', 'kdns@eunet.rs', '265-879871989-34')");
        }

        [TestInitialize]
        public void SetUp()
        {
            Bootstrapper.With.AutoMapper().Start();
            m_Repository = new KinoloskaDrustvaRepository();
        }

        [TestMethod]
        public void Insert()
        {
            using (new TransactionScope())
            {
                // Arrange
                var kd = new KinoloskoDrustvo
                         {
                             Naziv = "KS BEOGRAD",
                             Telefon = "011-344-442",
                             EMail = "kss@gmail.com",
                             TekuciRacun = "220-3423444-90",
                             Adresa = "Bul. Mihajla Pupina 66",
                             IdMesta = 1
                         };

                // Act
                m_Repository.Sacuvaj(kd);
                
                // Assert
                IEnumerable<KinoloskoDrustvo> svaDrustva = m_Repository.VratiSve();
                Assert.IsTrue(svaDrustva.Any(x => x.Naziv == "KS BEOGRAD"));
                var kdSnimljen = svaDrustva.Single(x => x.Naziv == "KS BEOGRAD");
                Assert.AreEqual(kd.Naziv, kdSnimljen.Naziv);
                Assert.AreEqual(kd.Telefon, kdSnimljen.Telefon);
                Assert.AreEqual(kd.Faks, kdSnimljen.Faks);
                Assert.AreEqual(kd.EMail, kdSnimljen.EMail);
                Assert.AreEqual(kd.TekuciRacun, kdSnimljen.TekuciRacun);
                Assert.AreEqual(kd.Adresa, kdSnimljen.Adresa);
            }
        }

        [TestMethod]
        public void Update()
        {
            using (new TransactionScope())
            {
                var existing = m_Repository.VratiPoId(1);
                existing.Naziv = "KSS";
                existing.Telefon = "011/711-678";
                existing.Faks = "011/711-678";
                existing.EMail = "kss@gmail.com";
                existing.TekuciRacun = "220-53453-45";
                existing.Adresa = "Preradovićeva 36";
                existing.IdMesta = 2;

                m_Repository = new KinoloskaDrustvaRepository();
                m_Repository.Sacuvaj(existing);

                m_Repository = new KinoloskaDrustvaRepository();
                var snimljeno = m_Repository.VratiPoId(1);
                Assert.AreEqual(existing.Naziv, snimljeno.Naziv);
                Assert.AreEqual(existing.Telefon, snimljeno.Telefon);
                Assert.AreEqual(existing.Faks, snimljeno.Faks);
                Assert.AreEqual(existing.EMail, snimljeno.EMail);
                Assert.AreEqual(existing.TekuciRacun, snimljeno.TekuciRacun);
                Assert.AreEqual(existing.Adresa, snimljeno.Adresa);
                Assert.AreEqual(existing.IdMesta, snimljeno.IdMesta);
            }
        }

        [TestMethod]
        public void VratiSve()
        {
            var sva = m_Repository.VratiSve();
            Assert.AreEqual(1, sva.Count());
            var prvo = sva.First();
            Assert.AreEqual(prvo.Naziv, "KD NOVI SAD");
        }
    }
}